package com.wilmer.videoOrder.service.impl;

import java.util.List;
import java.util.Objects;

import com.wilmer.common.exception.ServiceException;
import com.wilmer.common.utils.DateUtils;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.pay.utils.OrderUtil;
import com.wilmer.video.domain.Video;
import com.wilmer.video.mapper.VideoMapper;
import com.wilmer.videoOrder.domain.dto.VideoOrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wilmer.videoOrder.mapper.VideoOrderMapper;
import com.wilmer.videoOrder.domain.VideoOrder;
import com.wilmer.videoOrder.service.IVideoOrderService;

/**
 * 视频订单Service业务层处理
 * 
 * @author jett
 * @date 2024-04-02
 */
@Service
public class VideoOrderServiceImpl implements IVideoOrderService 
{
    @Autowired
    private VideoOrderMapper videoOrderMapper;

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 查询视频订单（通过用户查询已购买视频）
     *
     * @param userId 用户ID
     * @return 视频订单
     */
    @Override
    public List<VideoOrder> selectVideoOrderByUserId(Long userId)
    {
        return videoOrderMapper.selectVideoOrderByUserId(userId);
    }

    /**
     * 查询视频订单
     * 
     * @param id 视频订单主键
     * @return 视频订单
     */
    @Override
    public VideoOrder selectVideoOrderById(Long id)
    {
        return videoOrderMapper.selectVideoOrderById(id);
    }

    /**
     * 查询视频订单列表
     * 
     * @param videoOrder 视频订单
     * @return 视频订单
     */
    @Override
    public List<VideoOrder> selectVideoOrderList(VideoOrder videoOrder)
    {
        return videoOrderMapper.selectVideoOrderList(videoOrder);
    }

    /**
     * 新增视频订单
     * 
     * @param videoOrder 视频订单
     * @return 结果
     */
    @Override
    public int insertVideoOrder(VideoOrder videoOrder)
    {
        videoOrder.setCreateTime(DateUtils.getNowDate());
        return videoOrderMapper.insertVideoOrder(videoOrder);
    }

    /**
     * 修改视频订单
     * 
     * @param videoOrder 视频订单
     * @return 结果
     */
    @Override
    public int updateVideoOrder(VideoOrder videoOrder)
    {
        return videoOrderMapper.updateVideoOrder(videoOrder);
    }

    /**
     * 批量删除视频订单
     * 
     * @param ids 需要删除的视频订单主键
     * @return 结果
     */
    @Override
    public int deleteVideoOrderByIds(Long[] ids)
    {
        return videoOrderMapper.deleteVideoOrderByIds(ids);
    }

    /**
     * 删除视频订单信息
     * 
     * @param id 视频订单主键
     * @return 结果
     */
    @Override
    public int deleteVideoOrderById(Long id)
    {
        return videoOrderMapper.deleteVideoOrderById(id);
    }

    @Override
    public String createVideoOrder(VideoOrderDto dto) {
        // 生成订单编号
        String orderNumber = OrderUtil.generateOrderNumber();
        // 获取用户id
        Long userId = SecurityUtils.getUserId();
        // 对比订单金额
        Video video = videoMapper.selectVideoById(dto.getVideoId());
        if(video == null){
            throw new ServiceException("视频信息不存在或已删除",201);
        }
        if(video.getPrice().compareTo(dto.getPayAmount()) != 0){
            throw new ServiceException("视频金额不正确",201);
        }
        if(Objects.equals(video.getType(), "1")){
            throw new ServiceException("免费视频无需非付费",201);
        }
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoId(dto.getVideoId());
        videoOrder.setOrderNum(orderNumber);
        videoOrder.setPay(0);
        videoOrder.setPayAmount(video.getPrice());
        videoOrder.setUserId(userId);
        videoOrder.setCreateTime(DateUtils.getNowDate());
        videoOrderMapper.insertVideoOrder(videoOrder);

        return orderNumber;
    }
}
